#pragma once

#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include "Log.hpp"
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

namespace kzz
{
    class InetAddr
    {

    public:
    
        //主机转网络
        InetAddr(const std::string &ip, const uint16_t& port):_ip(ip),_port(port)
        {
            _addr.sin_family = AF_INET;
            _addr.sin_addr.s_addr = inet_addr(_ip.c_str());
            _addr.sin_port = htons(_port);
    
        }
        //专门用于服务器，接受所有IP，只传端口号
        InetAddr(const uint16_t& port):_port(port)
        {
            _addr.sin_family = AF_INET;
            _addr.sin_addr.s_addr = INADDR_ANY;
            _addr.sin_port = htons(_port);
             _ip=inet_ntoa(_addr.sin_addr) ;
        }
        //网络转主机
        InetAddr(const struct sockaddr_in& addr):_addr(addr)
        {
            _ip=inet_ntoa(_addr.sin_addr) ;
            _port=ntohs(_addr.sin_port) ;
        }
      const uint16_t& Port()
        {
            return _port;
            //return ntohs(_addr.sin_port) ;
        }
       const std::string& Ip()
        {
            return _ip;
            //return inet_ntoa(_addr.sin_addr) ;
        }
      struct sockaddr* Addr()
        {
            return (sockaddr*)&_addr;
        }
        socklen_t Len()
        {
            return _len;
        }



    private:
        uint16_t _port;
        std::string _ip;
        struct sockaddr_in _addr;
        socklen_t _len=sizeof(_addr);
    };
}